
#include "Plane.h"
#include "..\Graphics\DXManager.h"
#include "..\Systems\Logger.h"
#include "..\Systems\BasicWindow.h"

extern DXManager* gpDX;
extern BasicWindow* gpBW;

const int PLANE_IMG_WIDTH = 128;
const int PLANE_IMG_HEIGHT = 128;
const int PLANE_IMG_LEFT_OFFSET = 27;
const int PLANE_IMG_RIGHT_OFFSET = 28;
const int PLANE_IMG_TOP_OFFSET = 54;
const int PLANE_IMG_BOTTOM_OFFSET = 54;

const int MOVE_INC = 6;
const int PART_MOVE_INC = 4;
const int FIRING_DURATION = 10;

void Plane::Create(int x, int y)
{
	this->centerXPos = x;
	this->centerYPos = y;
	AdjustBounds();

	heading = NONE;

	texture = new Texture;
	texture->Create("resources\\textures\\centeredBigPlane.PNG", "plane", PLANE_IMG_WIDTH, PLANE_IMG_HEIGHT);

	firingTexture = new Texture;
	firingTexture->Create("resources\\textures\\firingPlane.PNG", "firingPlane", PLANE_IMG_WIDTH, PLANE_IMG_HEIGHT);

	sprite = new Sprite;
	sprite->Create("plane", texture);
	sprite->TranslateOriginal(imageXPos, imageYPos);
	Logger::getInstance().LOG(Logger::DEBUG, "Plane Created");

	//Logger::getInstance().LOG(Logger::DEBUG, "Texture1 = %d, text2 = %d, sprite = %d", texture, firingTexture, sprite);
	firingStage = 0;
}

void Plane::Destroy()
{
	texture->Destroy();
	delete texture;
	texture = NULL;

	firingTexture->Destroy();
	delete firingTexture;
	firingTexture = NULL;

	sprite->Destroy();
	delete sprite;
	sprite = NULL;
}

void Plane::Render()
{
	sprite->Draw();
}

void Plane::Update()
{
	switch (heading)
	{
	case NORTH:
		centerYPos -= MOVE_INC;
		break;
	case SOUTH:
		centerYPos += MOVE_INC;
		break;
	case EAST:
		centerXPos += MOVE_INC;
		break;
	case WEST:
		centerXPos -= MOVE_INC;
		break;
	case NORTHEAST:
		centerYPos -= PART_MOVE_INC;
		centerXPos += PART_MOVE_INC;
		break;
	case SOUTHWEST:
		centerYPos += PART_MOVE_INC;
		centerXPos -= PART_MOVE_INC;
		break;
	case SOUTHEAST:
		centerXPos += PART_MOVE_INC;
		centerYPos += PART_MOVE_INC;
		break;
	case NORTHWEST:
		centerXPos -= PART_MOVE_INC;
		centerYPos -= PART_MOVE_INC;
		break;
	}

	AdjustBounds();

	if (firingStage == 1)
	{
		sprite->SetTexture(firingTexture);
		firingStage++;
	}
	else if ((firingStage > 1) && (firingStage < FIRING_DURATION))
	{
		firingStage++;
	}
	else if (firingStage == FIRING_DURATION)
	{
		sprite->SetTexture(texture);
		firingStage = 0;
	}
	sprite->TranslateOriginal(imageXPos, imageYPos);
	sprite->Update();
}

void Plane::AdjustBounds()
{
	int windowHeight = gpBW->GetHeight();
	int windowWidth = gpBW->GetWidth();

	if (centerYPos-(PLANE_IMG_HEIGHT/2-PLANE_IMG_TOP_OFFSET) < 0)
	{
		centerYPos = (PLANE_IMG_HEIGHT/2-PLANE_IMG_TOP_OFFSET);
	}
	else if (centerYPos+(PLANE_IMG_HEIGHT/2-PLANE_IMG_BOTTOM_OFFSET) > windowHeight)
	{
		centerYPos = windowHeight-(PLANE_IMG_HEIGHT/2-PLANE_IMG_BOTTOM_OFFSET);
	}

	if (centerXPos-(PLANE_IMG_WIDTH/2-PLANE_IMG_LEFT_OFFSET) < 0)
	{
		centerXPos = (PLANE_IMG_WIDTH/2-PLANE_IMG_LEFT_OFFSET);
	}
	else if (centerXPos+(PLANE_IMG_WIDTH/2-PLANE_IMG_RIGHT_OFFSET) > windowWidth)
	{
		centerXPos = windowWidth-(PLANE_IMG_WIDTH/2-PLANE_IMG_RIGHT_OFFSET);
	}

	imageXPos = centerXPos - PLANE_IMG_WIDTH/2;
	imageYPos = centerYPos - PLANE_IMG_HEIGHT/2;
}
